package org.vehicle.iface;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.DriverInfo;
import org.vehicle.entity.TempPersonInfo;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.DriverInfoService;
import org.vehicle.service.TempPersonInfoService;
import org.vehicle.service.TripService;
import org.vehicle.service.VehicleInfoService;
import org.vehicle.service.redis.RedisService;
import org.vehicle.util.JsonUtils;
import org.vehicle.vo.TripInfoVO;

@Controller
@RequestMapping("interface/trip")
public class TripController {
	
	private static Logger logger = LogManager.getLogger(TripController.class.getName());
	
	@Autowired
	private VehicleInfoService vehicleInfoService;
	@Autowired
	private DriverInfoService driverInfoService;
	@Autowired
	private TripService tripService;
	@Autowired
	private TempPersonInfoService tempPersonInfoService;
	@Autowired
	private RedisService redisService;
	/**
	 * 起始或结束行程
	 * @param kcTripInfoVO
	 * @return
	 */
    @RequestMapping("startAndStopTrip")
    @ResponseBody
    public Map<String, Object> startAndStopTrip(@RequestBody TripInfoVO tripInfoVO) {
    	Map<String, Object> retMap=new HashMap<String, Object>();
    	try{
    		DriverInfo driverInfo=null;
    		if(tripInfoVO.getPersonInfoId()!=null){
    			TempPersonInfo tempPersonInfo=tempPersonInfoService.selectByPrimaryKey(tripInfoVO.getPersonInfoId());
    			driverInfo=driverInfoService.findDriverInfoByCertificateCode(tempPersonInfo.getCertificateCode());
    		}else{
    			driverInfo=driverInfoService.findDriverInfoByCertificateCode(tripInfoVO.getCertificateCode());
    		}
    		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    		
    		Map<String, Object> paramMap=new HashMap<String, Object>();
			paramMap.put("deviceSN", tripInfoVO.getDeviceCode());
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByCode(paramMap);
    		if(vehicleInfo==null){
    			retMap.put("state", 0);
    			retMap.put("msg","车辆不存在");
    			return retMap;
    		}
    		TripInfo tripInfo=new TripInfo();
    		tripInfo.setVehicleLicense(vehicleInfo.getVehicleLicense());
    		tripInfo= tripService.findTripInfoByVehicleLicense(tripInfo);
    		if(tripInfoVO.getTripState()!=0){
        		if(tripInfo!=null){
        			List<Map<String, Object>> personFaceDataList=tempPersonInfoService.findPersonFaceDataListByDeviceCode(tripInfoVO.getDeviceCode());
        			retMap.put("state", 1);
        			retMap.put("msg","行程已经开始");
        			retMap.put("personFaceDataList", personFaceDataList);
        			return retMap;
        		}
    		}else{
    			if(tripInfo==null){
    				retMap.put("state", -1);
        			retMap.put("msg","行程已经结束");
        			return retMap;
    			}
    		}
    		if(driverInfo==null){
    			retMap.put("state", 0);
    			retMap.put("msg","司机不存在");
    			return retMap;
    		}
    		tripInfo=new TripInfo();
    		tripInfo.setStartPassengerId(tripInfoVO.getStartPassengerId());
    		tripInfo.setStopPassengerId(tripInfoVO.getStopPassengerId());
    		tripInfo.setTripState(tripInfoVO.getTripState());
    		tripInfo.setVehicleLicense(vehicleInfo.getVehicleLicense());
    		tripInfo.setDriverInfoId(driverInfo.getDriverInfoId());
    		
    		TempPersonInfo tempPersonInfo=new TempPersonInfo();
    		tempPersonInfo.setPersonName(tripInfoVO.getName());
    		tempPersonInfo.setDeviceCode(tripInfoVO.getDeviceCode());
    		tempPersonInfo.setCertificateCode(tripInfoVO.getCertificateCode());
    		tempPersonInfo.setGender(tripInfoVO.getGender());
    		tempPersonInfo.setBirthday(sdf.parse(tripInfoVO.getBirthday()));
    		tempPersonInfo.setNationality(tripInfoVO.getNationality());
    		tempPersonInfo.setAddress(tripInfoVO.getAddress());
    		tempPersonInfo.setCertificateCodeEffectiveDate(tripInfoVO.getCertificateCodeEffectiveDate());
    		tempPersonInfo.setSignOrganization(tripInfoVO.getSignOrganization());
    		tempPersonInfo.setCertificateFaceBase64(tripInfoVO.getCertificateFaceBase64());
    		tempPersonInfo.setPersonFaceBase64(tripInfoVO.getPersonFaceBase64());
    		tempPersonInfo.setPersonFaceData(tripInfoVO.getPersonFaceData());
    		tempPersonInfo.setPersonType(2);
    		tempPersonInfo.setState(1);
    		tempPersonInfo.setCreateDate(new Date());
			tripService.startOrStopTrip(tripInfo,tempPersonInfo,vehicleInfo);
    		List<Map<String, Object>> personFaceDataList=tempPersonInfoService.findPersonFaceDataListByDeviceCode(tripInfoVO.getDeviceCode());
			if(tripInfoVO.getTripState()==0){
				redisService.removeString(vehicleInfo.getVehicleLicense());
				retMap.put("state", -1);
				retMap.put("msg","关闭成功");
				retMap.put("personFaceDataList", personFaceDataList);
			}else{
				Map<String, Object> positionMap=new HashMap<String, Object>();
	    		positionMap.put("tripId", tripInfo.getTripId());
	    		redisService.setString(vehicleInfo.getVehicleLicense(),JsonUtils.toJson(positionMap));
	    		redisService.setString(vehicleInfo.getVehicleLicense()+"_command","1");
				retMap.put("state", 1);
				retMap.put("msg","开启成功");
				retMap.put("personFaceDataList", personFaceDataList);
			}
			return retMap;
    	}catch(Exception e){
    		logger.error(e);
    	}
    	retMap.put("state", 0);
		retMap.put("msg","操作失败");
        return retMap;
    }
}
